//
"use strict"
// const dbase = require("../dbase");
const mvc = require("cosjs.mvc")
const dbase = mvc.model.require("dbase");

class collect extends dbase.gzone.mongodb {
    constructor(sid, uid) {
        super(sid, 'collect');
        this.uid = uid;
        this.ObjectID = ObjectID.bind(this);
    }
    uget(id) {
        let query = this.query(id), fields = arguments[1] || [];
        query["uid"] = this.uid;
        let option = { "multi": true, "fields": this.fields(fields) };
        return this.find(query, option);
    }
    //获取玩家道具列表
    mget(bag) {
        let query = { "uid": this.uid };
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        if (bag) {
            query["type"] = bag;
        }
        return this.find(query, option);
    }

    updateOne(_id, id, val, type) {
        let query = { "_id": _id };
        let option = { "upsert": true };
        // let option = {};
        let update = { "$setOnInsert": { "uid": this.uid, "id": id, "val": val, "type": type, "time": Date.now() } };
        return this.update(query, update, option);
    }


    // set(id){
    //     this.act('set',id,arguments[1],arguments[2]);
    // }

    // set(){
    //     let uid,id,type,val;
    //     if(arguments.length >= 3){
    //         uid = arguments[0];key = arguments[1];val = arguments[2];
    //     }
    //     else{
    //         uid = this.uid;key = arguments[0];val = arguments[1];
    //     }
    //     if(!uid){
    //         return Promise.reject('model.role.set uid empty');
    //     }
    //     return super.set(uid,key,val);
    // }

    apply(id, type, val) {
        // let time = Date.now();
        //甚至可以在这里初始化
        // let config = mvc.config.get("friend");
        // if(!config){
        //     return promise.callback("config_not_exist","friend");
        // }
        // let option = {"multi":false,"upsert":true};

        // //对方的好友列表
        // //关注模式需要提示对方，双向不需要确认直接成为好友，双向确认模式等待确认
        // let pv =1;
        // let pf = format_update.call(this,fid,uid,pv,time);
        // this.update(pf.query,pf.update,option);
        //保存

        let option = { "multi": false, "upsert": true };
        this.multi();
        let sf = format_update.call(this, this.uid, id, type, val);
        this.update(sf.query, sf.update, option);

        // this.multi();
        // let update = {"itemList":data};
        // let option = {"multi":false,"upsert":true};

        // this.update({"uid":uid},update,option);
        // if(config['apply'] > 0){
        //     this.update({"fid":uid,"val":-1},update,option);
        // }
        return this.save();

        // return this.save();
    }

    mpage(page, bag) {

        let $page = page || 1;
        let $size = 500;
        let $sort = '_id';
        let $order = 'desc';
        let dbsort = {};
        if ($sort) {
            dbsort[$sort] = $order == 'desc' ? -1 : 1;
        }
        let query = { "uid": this.uid };
        if (bag) {
            query["bag"] = bag;
        }
        let option = { "multi": true, "dataType": "array", "limit": 3000, 'batchSize': 3000 };
        return this.page(query, $page, $size, dbsort, option);

    }



}



module.exports = function (sid, uid) {
    return new collect(sid, uid);
};

//索引
module.exports.indexes = [
    [{ "uid": 1, "type": 1 }, {}]
]

function ObjectID(uid, id) {
    if (String(uid).indexOf("-") > 0) {
        return uid;
    }
    return [uid, id].join("-");
}

function format_update(uid, id, type, val) {
    let query = { "_id": ObjectID(uid, id) }
    let update = { "$set": { "uid": uid, "id": id, "type": type, "val": val } };
    return { "query": query, "update": update };
}
